<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
	private $languages;
    public function __construct()
    {
        parent::__construct();
        $this->config->db_config_fetch();
        $this->lang->loadDBLang();
        $lang = $this->languages_model->get_by_params(array('key'=> $this->lang->lang() ))->row();
        $this->config->set_item('lang_obj', $lang);	
        $this->languages = $this->languages_model->get_by_params()->result();

		if (!$this->session->userdata('auto')){
			$this->load->model('country_model');
			$this->load->model('currency_model');
			$countries = $this->country_model->get_by_params(array('language_id' => $lang->id))->result();
			if (count($countries)){
				$this->session->set_userdata(array('selected_country' => $countries[0]->id));
				
				if ($countries[0]->currency_id){
					$currency = $this->currency_model->get_by_id($countries[0]->currency_id);
					$this->session->set_userdata(array('selected_currency' => $currency->name));
				}
			}
		}
        
        $global_array = array();
        $global_array['languages'] = $this->languages;
        $global_array['currencies'] = $this->currency_model->get_by_params()->result();
        $global_array['countries'] = $this->country_model->get_by_params()->result();
        $global_array['langObject'] = $this->lang;   
        $global_array['sel_country'] = $this->getSelectedCountry();
        $global_array['sel_currency'] = $this->getSelectedCurrency(); 
        $global_array['access']	= $this->access;		
    
        $this->load->vars($global_array);     
    }	
	
	protected function _handle_if_post($type)
	{
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

		if( $this->input->is_post())
		{
			$this->_handle_post($type);
		}
	}

	protected function getSelectedCountry() {
		return ($this->session->userdata('selected_country')) ? $this->session->userdata('selected_country') : $this->config->item('def_country');	
	}
	
	public function getSelectedCurrency() {
		return ($this->session->userdata('selected_currency')) ? $this->session->userdata('selected_currency') : $this->config->item('def_currency');
	}	

}

class MY_Front_Controller extends MY_Controller {
    public function __construct() {
        parent::__construct();
        $global_array['exchange'] = $this->exchange;
        $global_array['controller'] = $this;
        $this->load->vars($global_array);
        
        $this->template->set_template('front');
        //$this->template->write('pageTitle', lang('site_title',false));
		$this->template->write_view ( 'header', 'templates/front/header.php');
		
		$menu = $this->category_model->get_menu();
		
		$start = substr($menu, 0, strpos($menu,">")+1);
		$end = substr($menu, -5);
		
		$menu = substr($menu,strpos($menu,">")+1,-5);
		
		$start .= "<li><div><a class='menu-item' href='" . site_url('main/') . "'>" . lang('home') . "</a></div></li>";
		
		$helpUl = "<ul class='category-1'>
				<li><div><a class='menu-item' href='" . site_url('main/page/faq_view') . "'>" . lang('faq_info') . "</a></div></li>
				<li><div><a class='menu-item' href='" . site_url('main/page/four_c_view') . "'>" . lang('four_c') . "</a></div></li>
				<li><div><a class='menu-item' href='" . site_url('main/page/valuations_view') . "'>" . lang('valuations') . "</a></div></li>
				<li><div><a class='menu-item' href='" . site_url('main/page/certifications_view') . "'>" . lang('certifications') . "</a></div></li>
				<li><div><a class='menu-item' href='" . site_url('main/page/returns_view') . "'>" . lang('returns_resizing') . "</a></div></li>
				<li><div><a class='menu-item' href='" . site_url('main/page/besboke_design_view') . "'>" . lang('besboke_design') . "</a></div></li>
				<li><div><a class='menu-item' href='" . site_url('main/page/metal_education_view') . "'>" . lang('metal_education') . "</a></div></li>
				<li><div><a class='menu-item' href='" . site_url('opinion') . "'>" . lang('testimonials') . "</a></div></li>
				<li><div><a class='menu-item' href='" . site_url('main/page/terms_view') . "'>" . lang('terms_conditions') . "</a></div></li>
				</ul>";
		
		$end = "<li><div><a class='menu-item' href='" . site_url('main/page/help') . "'>" . strtoupper(lang('help_label')) . "</a>" . $helpUl . "</div></li>" . $end;
		
		$menu = $start . $menu . $end;
		
		$this->template->write_view ( 'menu', 'templates/front/menu.php', array('menu' => $menu));
		$this->template->write_view ( 'footer', 'templates/front/footer.php');
        $javascriptSettings = "var settings = {};";
        $javascriptSettings.= "settings.base_url = '".base_url()."';";
        $javascriptSettings.= "settings.base_path = '".BASEPATH."';";
        $this->template->add_js(str_replace('\\','/',$javascriptSettings),'embed');
        $this->template->add_js(get_javascript_lang(),'embed');
        
             
    }
    
	public function getNumRows($cat_id, $filter_string) {
		return 1;
		$filter = array();
		$pre_filter = array();
		if (strlen($filter_string)){
			$pre_filter = explode(';',html_entity_decode(urldecode($filter_string)));

			if (isset($pre_filter[0])) {
				$firstElementArr = explode('/', $pre_filter[0]);
				$pre_filter[0] = end($firstElementArr);
			}
			foreach ($pre_filter as $key=>$value){
				$filter_tags = explode("=",$value);
				if ((isset($filter_tags[1]))) {
					$filter[$filter_tags[0]] = $filter_tags[1];
				}
				
			}
	
		}
		$search = $this->getFilters($filter,$cat_id);
		$this->db->cache_on();
		$total = $this->product_model->getProductList($search, true)->result();
		$this->db->stop_cache();
		return (int) $total[0]->cnt;
	}  

	public function getFilters($filter = array(),$cat_id = 0){
		$search = array();
		$search['current_url'] = current_url();
		$exchange = new Exchange();
	
		if (isset($filter['price']) AND strlen($filter['price'])){
			$price_arr = explode(',',$filter['price']);
	
			if ($price_arr[0]){
				$search['price_more_orig'] = $price_arr[0];
				$search['price_more'] = (($this->session->userdata('selected_currency') == 'GBP') ? $price_arr[0] : $exchange->transformPrice($price_arr[0],'GBP') );
			}
			if (isset($price_arr[1]) && $price_arr[1]){
				if ($this->session->userdata('selected_currency') == 'GBP'){
					$gbp_value = $exchange->transformPrice($price_arr[1],'GBP');
				}
				
				$search['price_less_orig'] = $price_arr[1];
				$search['price_less'] = (($this->session->userdata('selected_currency') == 'GBP') ? $price_arr[1] : $exchange->transformPrice($price_arr[1],'GBP') );
			}
			
			$this->load->model('country_model');
			$britain = $this->country_model->get_by_id(1);
			$search['brit_vat'] = 1+($britain->vat/100);
			
			$selected_country = $this->country_model->get_by_id($this->session->userdata('selected_country'));
			if ($selected_country){
				if($selected_country->vat){
					$search['current_vat'] = $selected_country->vat;
				} else {
					$search['current_vat'] = 0;
				}
			} else {
				$search['current_vat'] = 0;
			} 
		} else {
			if (isset($search['price_more'])){
				unset($search['price_more']);
				unset($search['price_more_orig']);
			}
		if (isset($search['price_less'])){
				unset($search['price_less']);
				unset($search['price_less_orig']);
			}
		}
		
		if ($cat_id){
			$search['category'] = $cat_id;
		} else {
			if (isset($search['category'])){
				unset($search['category']);
			}
		}
		
		if (isset($filter['metal']) AND strlen($filter['metal'])){
			$search['metal'] = $filter['metal'];
		} else {
			if (isset($search['metal'])){
				unset($search['metal']);
			}
		}
	
		if (isset($filter['gem']) AND strlen($filter['gem'])){
			$search['gem'] = $filter['gem'];
		} else {
			if (isset($search['gem'])){
				unset($search['gem']);
			}
		}
		
		if (isset($filter['gem']) AND strlen($filter['gem'])){
			$search['gem'] = $filter['gem'];
		} else {
			if (isset($search['gem'])){
				unset($search['gem']);
			}
		}
		
		if (isset($filter['gem']) AND strlen($filter['gem'])){
			$search['gem'] = $filter['gem'];
		} else {
			if (isset($search['gem'])){
				unset($search['gem']);
			}
		}
		
		if (isset($filter['related_to']) AND strlen($filter['related_to'])){
			$search['related_to'] = $filter['related_to'];
		} else {
			if (isset($search['related_to'])){
				unset($search['related_to']);
			}
		}
		
		if (isset($filter['gem']) AND strlen($filter['gem'])){
			$search['gem'] = $filter['gem'];
		} else {
			if (isset($search['gem'])){
				unset($search['gem']);
			}
		}
		
		if (isset($filter['style']) AND strlen($filter['style'])){
			$search['style'] = $filter['style'];
			if ($cat_id AND $this->config->item('lang_obj')->id != $this->config->item('def_language_id')){
				$categ = $this->category_model->get($cat_id);
				$search['original_id'] = $categ->original_id;
				$search['category_parent'] = $categ->original_id;
			}
		} else {
			if (isset($search['style'])){
				unset($search['style']);
			}
		}

		if (isset($filter['shape']) AND strlen($filter['shape'])){
			$search['shape'] = $filter['shape'];
		} else {
			if (isset($search['shape'])){
				unset($search['shape']);
			}
		}
		
		if (isset($filter['quality']) AND strlen($filter['quality'])){
			$search['quality'] = $filter['quality'];
		} else {
			if (isset($search['quality'])){
				unset($search['quality']);
			}
		}
		
				
		if (isset($filter['size']) AND strlen($filter['size'])){
			$search['size'] = $filter['size'];
			
			$size_arr = explode('-',$filter['size']);
			
			$search['size_more'] = $size_arr[0];
			if (isset($size_arr[1])){
				$search['size_less'] = $size_arr[1];
			}
		} else {
			if (isset($search['size'])){
				unset($search['size']);
			}
		}
		
		if (isset($filter['setting']) AND strlen($filter['setting'])){
			$search['setting'] = $filter['setting'];
		} else {
			if (isset($search['setting'])){
				unset($search['setting']);
			}
		}
		
		if (isset($filter['color']) AND strlen($filter['color'])){
			$search['color'] = $filter['color'];
		} else {
			if (isset($search['color'])){
				unset($search['color']);
			}
		}
		
		if (isset($filter['per_page']) AND strlen($filter['per_page'])){
			$search['per_page'] = $filter['per_page'];
		} else {
			if (isset($search['per_page'])){
				unset($search['per_page']);
			}
		}
		
		if (isset($filter['search']) AND strlen($filter['search'])){
			$search['search'] = $filter['search'];
		} else {
			if (isset($search['search'])){
				unset($search['search']);
			}
		}
		
		if (isset($filter['promotion']) AND strlen($filter['promotion'])){
			$search['promotion'] = $filter['promotion'];
		} else {
			if (isset($search['promotion'])){
				unset($search['promotion']);
			}
		}
		
		if (isset($filter['order_by']) AND strlen($filter['order_by'])){
			$search['order_by'] = $filter['order_by'];
		} else {
			$search['order_by'] = 'name';
		}
		
		if (isset($filter['order_dir']) AND strlen($filter['order_dir'])){
			$search['order_dir'] = $filter['order_dir'];
		} else {
			$search['order_dir'] = 'asc';
		}
		
		//$search['not_parent_id'] = 0;
		$search['group_by'] = 'parent_id';
		
		return $search;
	}	

} // End MY_Front_controller class

class MY_Admin_Controller extends MY_Controller {
	public function __construct()
	{
		
		parent::__construct();
		$this->template->set_template('admin');
	

		$method 	= $this->router->method;
		$class 		= $this->router->class;
		

		if($this->simpleloginsecure->is_logged_in(1) === FALSE) 
		{
			if($method !== 'login')
			{
				redirect("admin/login");
			}
		}
		else if($method === 'login')
		{
			$lang = $this->languages_model->get_by_id($this->access->getUserLang());
			redirect(base_url('/' . $lang->key . '/admin/synced_products'));
		}



		// logout
		//$this->simpleloginsecure->logout();

		// delete by user ID
		//$this->simpleloginsecure->delete($user_id); 
	}

} // End Admin class

/* End of file MY_Controller.php */
/* Location: ./application/libraries/MY_Controller.php */
